package com.tencreat.service.mall.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tencreat.common.core.domain.model.ReqIdParam;
import com.tencreat.common.core.domain.model.ReqIdsParam;
import com.tencreat.domain.mall.MallCoupon;
import com.tencreat.domain.mall.MallCouponUser;
import com.tencreat.mapper.mall.MallCouponUserMapper;
import com.tencreat.service.mall.IMallCouponUserService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 优惠券用户使用Service业务层处理
 *
 * @author tencreat
 * @date 2021-08-13
 */
@Service
public class MallCouponUserServiceImpl extends ServiceImpl<MallCouponUserMapper, MallCouponUser> implements IMallCouponUserService {


    /**
     * 查询优惠券用户使用
     *
     * @param id 优惠券用户使用ID
     * @return 优惠券用户使用
     */
    @Override
    public MallCouponUser selectMallCouponUserById(ReqIdParam id) {
        return baseMapper.selectById(id.getId());
    }

    /**
     * 查询优惠券用户使用列表
     *
     * @param mallCouponUser 优惠券用户使用
     * @return 优惠券用户使用
     */
    @Override
    public List<MallCouponUser> selectMallCouponUserList(MallCouponUser mallCouponUser) {
        return baseMapper.selectList(Wrappers.query(mallCouponUser));
    }

    /**
     * 新增优惠券用户使用
     *
     * @param mallCouponUser 优惠券用户使用
     * @return 结果
     */
    @Override
    public int insertMallCouponUser(MallCouponUser mallCouponUser) {
        mallCouponUser.setCreateTime(LocalDateTime.now());
        //优惠券发放

        return baseMapper.insert(mallCouponUser);
    }

    /**
     * 修改优惠券用户使用
     *
     * @param mallCouponUser 优惠券用户使用
     * @return 结果
     */
    @Override
    public int updateMallCouponUser(MallCouponUser mallCouponUser) {
        mallCouponUser.setUpdateTime(LocalDateTime.now());
        return baseMapper.updateById(mallCouponUser);
    }

    /**
     * 批量删除优惠券用户使用
     *
     * @param ids 需要删除的优惠券用户使用ID
     * @return 结果
     */
    @Override
    public int deleteMallCouponUserByIds(ReqIdsParam ids) {
        return baseMapper.deleteBatchIds(ids.getId());
    }

    /**
     * 删除优惠券用户使用信息
     *
     * @param id 优惠券用户使用ID
     * @return 结果
     */
    @Override
    public int deleteMallCouponUserById(ReqIdParam id) {
        return baseMapper.deleteById(id.getId());
    }

    @Override
    public List<MallCoupon> getCouponByUuid(Long uuid) {
        return baseMapper.getCouponByUuid(uuid,LocalDateTime.now());
    }

    @Override
    public void grantCoupon(MallCoupon mallCoupon) {
         baseMapper.grantCoupon(mallCoupon);
    }

    @Override
    public void grantNewUserCoupon(Long uuid) {
        baseMapper.grantNewUserCoupon(uuid,LocalDateTime.now());
    }

    @Override
    public void updateState(Long id, Long uuid,Long status) {
        baseMapper.updateState(id,uuid, status);
    }

    @Override
    public List<MallCouponUser> getUserAllCoupon(Long uuid, Long status) {
        return baseMapper.getUserAllCoupon(uuid, status);
    }

    @Override
    public List<MallCoupon> getCouponByUuidV2(Long uuid,Integer usedStatus) {
        List<MallCoupon> couponByUuidV2 = baseMapper.getCouponByUuidV2(uuid, LocalDateTime.now(), usedStatus);
        for (MallCoupon coupon:couponByUuidV2){
            // 计算百分比
            BigDecimal divide = BigDecimal.valueOf(coupon.getSurplus()).divide(BigDecimal.valueOf(coupon.getTotal()), 2, BigDecimal.ROUND_HALF_DOWN);
            if (divide.doubleValue() >= 1){
                coupon.setReceived(0);
            }else if (divide.doubleValue() <= 0){
                coupon.setReceived(100);
            }else {
                BigDecimal multiply = BigDecimal.valueOf(1).subtract(divide).multiply(BigDecimal.valueOf(100));
                coupon.setReceived(multiply.intValue());
            }
        }

        return couponByUuidV2;
    }
}
